package thing

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"iot-base/auth/common"
	Log "iot-base/auth/logger"
	types2 "iot-base/auth/types"
	"iot-base/common/ctrltype"
)

// AccessControl 门禁.
func AccessControl(c *gin.Context) {
	requestId, _ := c.Get("RequestId")
	uid := c.GetInt64("uid")
	resp := types2.NewResult(c)
	var req types2.ReqAccessControl
	if err := c.ShouldBind(&req); err != nil {
		Log.Log.Errorf("[%s] [%v] err:=%v", requestId, uid, err)
		resp.ErrorCode(types2.ParamsError, "")
		return
	}
	params := make(map[string]interface{})
	var cmd ctrltype.CtrlCmd
	if req.Status == "0" {
		cmd.ID = types2.AccessControlClose
	} else {
		cmd.ID = types2.AccessControlOpen
	}
	cmd.ThingID = req.ThingId
	cmd.Params = params
	fmt.Println("AccessControl cmd", cmd)
	Log.Log.Infof("[%s] [%v] AccessControl cmd %v", requestId, uid, cmd)
	result, err := common.CtrlResp(&cmd)
	Log.Log.Infof("[%s] [%v] AccessControl err %v result %v", requestId, uid, err, result)
	if err == nil {
		resp.Success(result)
	} else {
		resp.ErrorCode(types2.Fail, "")
	}
	return
}

func Fan(c *gin.Context) {
	requestId, _ := c.Get("RequestId")
	uid := c.GetInt64("uid")
	resp := types2.NewResult(c)
	var req types2.ReqAIDA
	if err := c.ShouldBind(&req); err != nil {
		Log.Log.Errorf("[%s] [%v] err:=%v", requestId, uid, err)
		resp.ErrorCode(types2.ParamsError, "")
		return
	}
	params := make(map[string]interface{})
	var cmd ctrltype.CtrlCmd
	if req.Status == "0" {
		params["status"] = req.Status
	} else {
		params["status"] = req.Status
	}
	cmd.ID = types2.FanSwitch
	cmd.ThingID = req.ThingId
	cmd.Params = params
	result, err := common.CtrlResp(&cmd)
	fmt.Println("Fan", cmd)
	Log.Log.Infof("[%s] [%v] aidi cmd %v", requestId, uid, cmd)
	Log.Log.Infof("[%s] [%v] aidi err %v result %v", requestId, uid, err, result)
	if err == nil {
		resp.Success(result)
	} else {
		resp.ErrorCode(types2.Fail, "")
	}
	return
}

func Switch(c *gin.Context) {
	requestId, _ := c.Get("RequestId")
	uid := c.GetInt64("uid")
	resp := types2.NewResult(c)
	var req types2.ReqSwitch
	if err := c.ShouldBind(&req); err != nil {
		Log.Log.Errorf("[%s] [%v] err:=%v", requestId, uid, err)
		resp.ErrorCode(types2.ParamsError, "")
		return
	}
	// 服务ID
	serviceId, ok := types2.SwitchService[req.ModelId]
	if !ok {
		Log.Log.Errorf("[%s] [%v] err:=%v", requestId, uid, "serviceId not foud")
		resp.ErrorCode(types2.ParamsError, "")
		return
	}
	params := make(map[string]interface{})
	var cmd ctrltype.CtrlCmd
	if req.Status == "0" {
		params["status"] = req.Status
	} else {
		params["status"] = req.Status
	}

	cmd.ID = serviceId
	cmd.ThingID = req.ThingId
	cmd.Params = params
	result, err := common.CtrlResp(&cmd)
	fmt.Println("Fan", cmd)
	Log.Log.Infof("[%s] [%v] aidi cmd %v", requestId, uid, cmd)
	Log.Log.Infof("[%s] [%v] aidi err %v result %v", requestId, uid, err, result)
	if err == nil {
		resp.Success(result)
	} else {
		resp.ErrorCode(types2.Fail, "")
	}
	return
}

// AIDI 门禁.
//func AIDI(c *gin.Context) {
//	requestId, _ := c.Get("RequestId")
//	uid := c.GetInt64("uid")
//	resp := types.NewResult(c)
//	var req types.ReqAIDA
//	if err := c.ShouldBind(&req); err != nil {
//		Log.Log.Errorf("[%s] [%v] err:=%v", requestId, uid, err)
//		resp.ErrorCode(types.ParamsError, "")
//		return
//	}
//	params := make(map[string]interface{})
//	var cmd ctrltype.CtrlCmd
//	if req.Status == "0" {
//		params["status"] = req.Status
//	} else {
//		params["status"] = req.Status
//	}
//	cmd.ID = types.AIDI
//	cmd.ThingID = req.ThingId
//	cmd.Params = params
//	result, err := CtrlResp(&cmd)
//	fmt.Println("aidi", cmd)
//	Log.Log.Infof("[%s] [%v] aidi cmd %v", requestId, uid, cmd)
//	Log.Log.Infof("[%s] [%v] aidi err %v result %v", requestId, uid, err, result)
//	if err == nil {
//		resp.Success(result)
//	} else {
//		resp.ErrorCode(types.Fail, "")
//	}
//	return
//}
